package com.jt.tenant.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetChangePwd;
import com.zcx.helper.http.AsyCallBack;

/**
 * Created by yzf on 2016/2/14.
 */
public class PwdChangeActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private EditText old_pwd, change_new_pwd1, change_new_pwd2;

    private TextView commit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_pwd_change);

        init_view();
    }

    private void init_view() {

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("修改密码");

        old_pwd = (EditText) findViewById(R.id.old_pwd);

        change_new_pwd1 = (EditText) findViewById(R.id.change_new_pwd1);

        change_new_pwd2 = (EditText) findViewById(R.id.change_new_pwd2);

        (commit = (TextView) findViewById(R.id.tv_changepwd_commit)).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            //提交
            case R.id.tv_changepwd_commit:

                String str_old_pwd = old_pwd.getText().toString().trim();

                String str_new_pwd1 = change_new_pwd1.getText().toString().trim();

                String str_new_pwd2 = change_new_pwd2.getText().toString().trim();

                if (str_old_pwd.equals("")) {

                    Toast.makeText(this, "请输入您的原始密码", Toast.LENGTH_SHORT).show();
                } else if (str_new_pwd1.equals("") || str_new_pwd2.equals("")) {

                    Toast.makeText(this, "请输入您的新密码", Toast.LENGTH_SHORT).show();
                } else if (!str_new_pwd1.equals(str_new_pwd2)) {

                    Toast.makeText(this, "两次输入新密码不一致", Toast.LENGTH_SHORT).show();
                } else if (str_new_pwd1.length() < 6 || str_new_pwd1.length() > 20) {

                    Toast.makeText(this, "新密码规格不符合要求", Toast.LENGTH_SHORT).show();
                } else {

                    new GetChangePwd(JTApplication.jtPreferences.readPhone(), str_old_pwd, str_new_pwd1, new AsyCallBack<GetChangePwd.Info>() {

                        @Override
                        public void onSuccess(String toast, int type, GetChangePwd.Info info) throws Exception {

                            if (info.msg.equals("000")) {

                                Toast.makeText(context, "密码修改成功！", Toast.LENGTH_SHORT).show();

                                //uid设置为false
                                JTApplication.jtPreferences.saveHaveUid(false);
                                //uid清除
                                JTApplication.jtPreferences.saveUserId("");
                                //token清除
                                JTApplication.jtPreferences.saveToken("");

                                startActivity(new Intent(context, LoginActivity.class));

                                JTApplication.INSTANCE.finishActivity(MainActivity.class);

                                finish();

                            } else {

                                Toast.makeText(context, info.msg, Toast.LENGTH_SHORT).show();
                            }

                        }
                    }).execute(this);
                }

                break;
        }
    }
}
